﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Reflection;
using System.Web.SessionState;

namespace MagicBag.CRM.HttpHandler
{
    /// <summary>
    /// httpHandler处理类，
    /// 注：引用命名空间【System.Web.SessionState】并实现IReadOnlySessionState是为了在AjaxHandler及实现它的类中读取Sesion
    /// 如果要写Session还要实现IRequiresSessionState
    /// IReadOnlySessionState和IRequiresSessionState接口都是没有方法的，只是一个标记接口，没有任何方法
    /// </summary>
    public class AjaxHandler : IHttpHandler, IReadOnlySessionState
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string assemblyDirectory = AppDomain.CurrentDomain.BaseDirectory + @"Bin\";
            string[] sections = context.Request.AppRelativeCurrentExecutionFilePath.Split('/');

            int n = sections.Length;
            string assembly = sections[n - 2];
            int length = sections[n - 1].LastIndexOf('.');
            string className = sections[n - 1].Substring(0, length);
            IMagicBagAjaxService service = null;
            //请求的方法所在的类必须继承IMagicBagAjaxService
            string assemblPath = assemblyDirectory + assembly + ".dll";//绝对路径
            service = (IMagicBagAjaxService)Assembly.LoadFrom(assemblPath).CreateInstance(assembly + "." + className, false, BindingFlags.CreateInstance, null, null, null, new object[0]);
            service.ResponseMessage(context);
        }
    }
}
